package org.ithasu.tools.net.http.impl;

import java.util.ArrayList;

import org.ithasu.helpers.Pair;
import org.ithasu.tools.net.http.SearchEngine;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class KickAssTorrent extends SearchEngine {

	public KickAssTorrent(String query) {
		super(query);
	}

	@Override
	protected String getQueryURL() {
		return "http://kat.ph/search/" + getEncodedQuery() + "/";
	}

	// --- Results ----------------------------------------

	public ArrayList<Pair<String, String>> getMagnetLinks() {
		Document doc = getDocument();
		ArrayList<Pair<String, String>> res = new ArrayList<>();
		Elements cells = doc.select(".torrentnameCell");
		for (Element cell : cells) {
			// Name
			Elements names = cell.select(".bold");
			if (names.isEmpty()) {
				continue;
			}
			String name = names.get(0).text();
			// Link
			Elements links = cell.select("a[href*=magnet");
			for (Element l : links) {
				res.add(new Pair<String, String>(name, l.attr("href")));
			}
		}
		return res;
	}
}
